import Koa from 'koa';
import session from 'koa-session';
import passport from 'koa-passport';
import bodyParser from 'koa-bodyparser';
import sequelize from './db';
import authRoutes from './routes/auth';
import './auth/local';
import './auth/github';
import './auth/jwt';
import path from 'path';
import views from 'koa-views';

const app = new Koa();

app.use(views(path.join(__dirname, 'views'), {
  extension: 'ejs'
}));

app.keys = ['your-secret-key'];
app.use(session({}, app));

app.use(bodyParser());

app.use(passport.initialize());
app.use(passport.session());

app.use(authRoutes.routes()).use(authRoutes.allowedMethods());

const PORT = process.env.PORT || 3000;

sequelize.sync().then(() => {
  console.log('Database synced');
  app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
  });
}).catch(err => {
  console.error('Database sync error:', err);
});

export default app;